package controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.User;
import service.UserManager;
import service.impl.UserManagerFactory;

@Controller
public class UserController {
	@Resource(name="umi1")
	private UserManager manager;
	
//	public UserController() {
//		manager = UserManagerFactory.getInstance();
//	}

	
	@RequestMapping(value="/addUser1")
	@PostMapping
	public String addUser1(User user ){
		manager.addUser(user);
		System.out.println(user.getBirth());
		return "redirect:/listAll1";
	}
	@RequestMapping(value="/listAll1")
	public String findALL1(Model model){
		List<User> users = manager.findAll();
		model.addAttribute("users", users);
		return "show_users";
	}
	@ResponseBody
	@RequestMapping(value="/addUser",consumes="application/json")
	@PostMapping
	public List<User> addUser(@RequestBody User user,Model model){
		System.out.println(user);
		manager.addUser(user);
		List<User> users = manager.findAll();
		return users;
	}
	@ResponseBody
	@RequestMapping(value="/updateUser",consumes="application/json")
	@PostMapping
	public List<User>updateUser(@RequestBody User user,Model model){
		manager.updataUser(user);
		List<User> users = manager.findAll();
		return users;
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteUser")
	public List<User> deleteUser(String id,Model model){
		manager.deleteUser(id);
		List<User> users = manager.findAll();
		return users;
	}
	@ResponseBody
	@RequestMapping(value="/findAllUsers",method=RequestMethod.GET)
	public List<User> findAllUser(){
		List<User> users = manager.findAll();
		return users;
	}
	
}
